package com.mymoney.data.access;

import android.util.Log;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mymoney.constants.Constants;
import com.mymoney.ws.data.ItemBean;
import com.mymoney.ws.data.RegisterDeviceResponse;

public class JsonParser {

    /** The only instance of the class. */
    private static JsonParser instance = new JsonParser();

    /** The parse to use for serialziation/deserialization to/from json. */
    private Gson gsonParser;

    private JsonParser() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat(Constants.COMMON_DATE_FORMAT);
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
        gsonParser = gsonBuilder.create();
    }

    /** Returns the only instance of the class. */
    public static JsonParser getInstance() {
        return instance;
    }



    public String toJSON(Object object) {
        String jsonString = gsonParser.toJson(object);
        Log.d("JSONParser", "toJSON: " + jsonString);
        return jsonString;
    }

    public RegisterDeviceResponse getRegisterDeviceBean(String registerDeviceResponse) {
        return gsonParser.fromJson(registerDeviceResponse, RegisterDeviceResponse.class);
    }
    
    public ItemBean[] getItemsResponce(String listItemsResponse) {
        return gsonParser.fromJson(listItemsResponse, ItemBean[].class);
    }
}
